package com.uzhie.media.service;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;

/**
 * Created by TangFei on 2018/7/28.
 */
public abstract class MediaService {

    protected int CODE_OK = 200;

    protected int CODE_ERROR = 500;

    protected enum STATUS {
        OK(200, "OK"), ERROR(500, "ERROR");
        int code;
        String msg;

        public int getCode() {
            return code;
        }

        public String getMsg() {
            return msg;
        }

        STATUS(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    }

    protected <T> JSONObject response(STATUS status, T data) {
        return response(status.getCode(), status.getMsg(), data);
    }

    protected <T> JSONObject response(int code, String msg, T data) {
        JSONObject object = new JSONObject();
        object.put("code", code);
        object.put("msg", Strings.isNullOrEmpty(msg) ? "null" : msg);
        JSONObject result = new JSONObject();
        result.put("data", null == data ? "null" : data);
        object.put("result", result);
        return object;
    }

}
